/*
    eflags generate code
    Copyright (C) 2011  Jiabo <jiabo2011@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "regs-map.h"
#include "../cpu.h"

/*
	T0 saves the original eflags
	T2 saves the result
*/
	.text
	.global gen_arm_flags_start, gen_arm_flags_end
gen_arm_flags_start:

#if defined(__arm__)

	and		T1, OPC, #3
	cmp		T1, #1
	sxtheq	OP1, OP1
	sxtheq	OP2, OP2
	sxtbmi	OP1, OP1
	sxtbmi	OP2, OP2

	mov		R0, #0

	bic		T1, OPC, #3
	add		pc, pc, T1
	mov		r0, r0	//nop
	b		none
	b		logic
	b		add
	b		cmp
	b		sub
	b		inc
	b		dec
	b		shl
	b		shr
	b		mul
none:
	b		op_end	//clear all flags
logic:
	mov		T2, OP1
	cmp		T2, #0
	b		op_end
cmp:
sub:
	subs		T2, OP1, OP2
	orrcc	R0, R0, #CC_C
	b		op_af
dec:
	tst		T0, #CC_C
	orrne	R0, R0, #CC_C
	subs		T2, OP1, #1
	b		op_af
add:
	adds		T2, OP1, OP2
	orrcs	R0, R0, #CC_C
	b		op_af
inc:
	tst		T0, #CC_C
	orrne	R0, R0, #CC_C
	adds		T2, OP1, #1
	b		op_af
shl:
shr:
mul:
op_af:
	eor		T1, OP1, OP2
	eor		T1, T2, T1
	and		T1, T1, #CC_A
	orr		R0, R0, T1

op_end:
	orrvs	R0, R0, #CC_O
	orreq	R0, R0, #CC_Z
	orrmi	R0, R0, #CC_S
#endif
gen_arm_flags_end:
